package com.ruoyi.mng.service.impl;

import java.util.List;
import java.util.function.Consumer;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mng.domain.HzChaChannel;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.mng.service.IHzChaChannelService;
import com.ruoyi.mng.service.IHzMerMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mng.mapper.HzOrderMapper;
import com.ruoyi.mng.domain.HzOrder;
import com.ruoyi.mng.service.IHzOrderService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 订单管理Service业务层处理
 *
 * @author chenqiang
 * @date 2023-04-29
 */
@Service
public class HzOrderServiceImpl implements IHzOrderService {
    @Autowired
    private HzOrderMapper hzOrderMapper;
    @Resource
    private IHzMerMerchantService hzMerMerchantService;
    @Resource
    private IHzChaChannelService hzChaChannelService;

    /**
     * 查询订单管理
     *
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public HzOrder selectHzOrderByOrderId(Long orderId) {
        HzOrder hzOrder = hzOrderMapper.selectHzOrderByOrderId(orderId);
        Consumer<HzOrder> hzOrderConsumer = this::setExtendHzOrder;
        hzOrderConsumer.accept(hzOrder);
        return hzOrder;
    }

    /**
     * 查询订单管理列表
     *
     * @param hzOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<HzOrder> selectHzOrderList(HzOrder hzOrder) {
        List<HzOrder> hzOrderList = hzOrderMapper.selectHzOrderList(hzOrder);
        hzOrderList.forEach(this::setExtendHzOrder);
        return hzOrderList;
    }

    private void setExtendHzOrder(HzOrder hzOrder) {
        hzOrder.setExtend_merchantName(hzOrder.getMerchantId() + " :: " + hzMerMerchantService.selectHzMerMerchantByMerchantId(hzOrder.getMerchantId()).getMerchantShortName());
        hzOrder.setExtend_busiName(hzOrder.getTranType() + " :: " + hzOrder.getTranName());
        HzChaChannel hzChaChannel = hzChaChannelService.selectHzChaChannelByChannelId(hzOrder.getChannelId());
        hzOrder.setExtend_channelName(hzChaChannel.getChannelName());
    }

    /**
     * 新增订单管理
     *
     * @param hzOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertHzOrder(HzOrder hzOrder) {
        hzOrder.setCreateTime(DateUtils.getNowDate());
        return hzOrderMapper.insertHzOrder(hzOrder);
    }

    /**
     * 修改订单管理
     *
     * @param hzOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateHzOrder(HzOrder hzOrder) {
        hzOrder.setUpdateTime(DateUtils.getNowDate());
        return hzOrderMapper.updateHzOrder(hzOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteHzOrderByOrderIds(String orderIds) {
        return hzOrderMapper.deleteHzOrderByOrderIds(Convert.toStrArray(orderIds));
    }

    /**
     * 删除订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteHzOrderByOrderId(Long orderId) {
        return hzOrderMapper.deleteHzOrderByOrderId(orderId);
    }
}
